package com.hundsun.documentripper.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hundsun.documentripper.domain.dto.BaseResponse;
import com.hundsun.documentripper.domain.dto.FileSource;
import com.hundsun.documentripper.domain.dto.FileView;
import com.hundsun.documentripper.domain.pojo.File;
import com.hundsun.documentripper.domain.query.FileQuery;
import com.hundsun.documentripper.error.ErrorCatcher;
import com.hundsun.documentripper.service.FileService;
import com.hundsun.documentripper.service.FileSourceService;
import com.hundsun.documentripper.service.FileViewService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * Note：
 *
 * @author ：mengjw
 * @description：文件控制层
 * @date ：Created in 2021/1/28
 */
@Api("文件")
@RestController
public class FileController {

    @Autowired
    private FileService fileService;
    @Autowired
    private FileViewService fileViewService;
    @Autowired
    private FileSourceService fileSourceService;

    @ApiOperation("新建文件")
    @PostMapping("/file/")
    @ErrorCatcher(code = "4001")
    @Transactional
    public BaseResponse<File> insertFile(@RequestBody @Valid File file, HttpServletRequest request) {
        file.setCreateUser((String) request.getSession().getAttribute("userUuid"));
        return new BaseResponse<File>(fileService.insertFile(file));
    }

    @ApiOperation("更新文件信息")
    @PutMapping("/file/")
    @ErrorCatcher(code = "4002")
    @Transactional
    public BaseResponse<Void> updateFile(@RequestBody File file, HttpServletRequest request) {
        file.setModifyUser((String) request.getSession().getAttribute("userUuid"));
        fileService.updateFile(file);
        return new BaseResponse<Void>();
    }

    @ApiOperation("根据唯一标识获取文件对象")
    @GetMapping("/file/{uuid}")
    @ErrorCatcher(code = "4003")
    public BaseResponse<File> getFileById(@PathVariable("uuid") String uuid) {
        return new BaseResponse<File>(fileService.getFileById(uuid));
    }

    @ApiOperation("删除文件")
    @DeleteMapping("/file/{uuid}")
    @ErrorCatcher(code = "4004")
    @Transactional
    public BaseResponse<Void> deleteFile(@PathVariable("uuid") String uuid) {
        fileService.deleteFile(uuid);
        return new BaseResponse<Void>();
    }

    @ApiOperation("获取对应产品版本的最新文件列表")
    @GetMapping("/productVersion/{productVersionUuid}/file")
    @ErrorCatcher(code = "4005")
    public BaseResponse<List> getFileListByVersion(@PathVariable("productVersionUuid") String productVersionUuid) {
        return new BaseResponse<List>(fileService.getFileListByVersion(productVersionUuid));
    }

    @ApiOperation("分页获取对应产品版本的最新文件列表")
    @PostMapping("/productVersion/file")
    @ErrorCatcher(code = "4010")
    public BaseResponse<IPage> getFileListByVersionByPage(@RequestBody @Valid FileQuery fileQuery) {
        return new BaseResponse<IPage>(fileService.getFileListByVersionByPage(fileQuery));
    }

    @ApiOperation("根据唯一标识获取文件展示对象")
    @ApiIgnore
    @GetMapping("/file/{uuid}/view")
    @ErrorCatcher(code = "4006")
    @Deprecated
    public BaseResponse<FileView> getFileViewById(@PathVariable("uuid") String uuid) {
        return new BaseResponse<FileView>(fileViewService.getFileViewById(uuid));
    }

    @ApiOperation("保存文件展示对象")
    @ApiIgnore
    @PostMapping("/file/view")
    @ErrorCatcher(code = "4007")
    @Transactional
    @Deprecated
    public BaseResponse<FileView> saveFileView(@RequestBody FileView fileView, HttpServletRequest request) {
        fileView.setCreateUser((String) request.getSession().getAttribute("userUuid"));
        return new BaseResponse<FileView>(fileViewService.saveFileView(fileView));
    }

    @ApiOperation("根据唯一标识获取文件元数据")
    @GetMapping("/file/{uuid}/source")
    @ErrorCatcher(code = "4008")
    public BaseResponse<FileSource> getFileSourceById(@PathVariable("uuid") String uuid) {
        return new BaseResponse<FileSource>(fileSourceService.getFileSourceById(uuid));
    }

    @ApiOperation("保存文件元数据")
    @PostMapping("/file/source")
    @ErrorCatcher(code = "4009")
    @Transactional
    public BaseResponse<FileSource> saveFileSource(@RequestBody FileSource fileSource, HttpServletRequest request) {
        fileSource.setCreateUser((String) request.getSession().getAttribute("userUuid"));
        return new BaseResponse<FileSource>(fileSourceService.saveFileSource(fileSource));
    }

}